home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 617 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: ix.netcom.com!netnews
  2. From: miker3@ix.netcom.com (Mike Rubenstein)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: start array at k, not 0
  5. Date: Mon, 08 Jan 1996 01:13:35 GMT
  6. Organization: Netcom
  7. Message-ID: <30f06d21.167303296@nntp.ix.netcom.com>
  8. References: <Pine.OSF.3.91.960104095358.22268B-100000@io.UWinnipeg.ca> <820787486snz@genesis.demon.co.uk> <Pine.OSF.3.91.960105091211.2861B-100000@io.UWinnipeg.ca> <4cpdt3$3b5@newdelph.cig.mot.com>
  9. NNTP-Posting-Host: ix-dc14-09.ix.netcom.com
  10. X-NETCOM-Date: Sun Jan 07  5:13:38 PM PST 1996
  11. X-Newsreader: Forte Agent .99c/16.141
  12.  
  13. "John R. Haug" <Haug_John@pcsmail.cig.mot.com> wrote:
  14.  
  15. |>I don't know if anyone else mentioned this, since I am just starting
  16. to 
  17. |>read this newsgroup, but the book, "Numerical Recipes in C: The Art
  18. of 
  19. |>Scientific Computing" published by Cambridge University Press
  20. describes 
  21. |>a great way of doing what I think you are looking for.  As a C and 
  22. |>FORTRAN programmer, I use their techniques extensively when writing 
  23. |>scientific code in C.
  24.  
  25. I don't think anyone has mentioned this yet on this thread.
  26.  
  27. Perhaps the reason is that the method proposed in that book is
  28. incorrect and results in undefined behavior.  I'd suggest you stop
  29. using it and stick to correct C.
  30.  
  31. For those not acquainted with the book, the authors recommend:
  32.  
  33.     One problem is that many algorithms naturally like to go from 
  34.     1 to M, not from 0 to M - 1.  Sure, you can always convert 
  35.     them, but they often acquire a baggage of additional 
  36.     arithmetic in array indices that is, at best, distracting.  It
  37.  
  38.     is better to use the power of the C language, in a consistent 
  39.     way, to make the problem disappear.  Consider
  40.  
  41.         float b[4], *bb;
  42.         bb = b - 1;
  43.  
  44.     The pointer bb now points one location before b.  A 
  45.     consequence is that the array elements bb[1], bb[2], bb[3], 
  46.     and bb[4] all exist.  In other words the range of bb is 
  47.     bb[1..4].  We wil refer to bb as a unit-offset vector.
  48.  
  49. It's unfortunate that the authors of this book didn't bother to learn
  50. C before writing it.  As anyone who has read the FAQ [6.13] knows,
  51. this results in undefined behavior.  The program is under no
  52. obligation to do anything sensible if you code like this.
  53.  
  54.  
  55. Michael M Rubenstein
  56.